package cn.sunshine.ymcc.web.controller;

import cn.sunshine.ymcc.domain.Course;
import cn.sunshine.ymcc.dto.CourseAddDto;
import cn.sunshine.ymcc.query.CourseQuery;
import cn.sunshine.ymcc.result.JSONResult;
import cn.sunshine.ymcc.result.PageList;
import cn.sunshine.ymcc.service.ICourseService;
import cn.sunshine.ymcc.vo.CourseDetailInfoVo;
import cn.sunshine.ymcc.vo.CourseInfosTotalAmountVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;
/*
* @Title: getCourseInfosTotalAmount
* @Description: 获取订单结算页的相关数据
* @Author: Mr.Jiang
* @Version: 1.0
* @Date:  2022/8/18 0:44
* @Parameters: [courseIds]
* @Return cn.sunshine.ymcc.result.JSONResult
*/
    @RequestMapping(value = "/info/{courseIds}",method = RequestMethod.GET)
    public JSONResult getCourseInfosTotalAmount(@PathVariable("courseIds") String courseIds) {
        CourseInfosTotalAmountVo courseInfosTotalAmountVo = courseService.selectCourseInfosTotalAmount(courseIds);
        return JSONResult.success(courseInfosTotalAmountVo);
    }
        /*
         * @Title: getdetailInfo
         * @Description: 课程详情
         * @Author: Mr.Jiang
         * @Version: 1.0
         * @Date:  2022/8/17 18:14
         * @Parameters: [courseId]
         * @Return cn.sunshine.ymcc.result.JSONResult
         */
        @RequestMapping(value = "/detailInfo/{courseId}", method = RequestMethod.GET)
        public JSONResult getdetailInfo (@PathVariable("courseId") Long courseId){
            CourseDetailInfoVo courseDetailInfoVo = courseService.getdetailInfo(courseId);
            return JSONResult.success(courseDetailInfoVo);
        }

    /*
    * @Title: onLineCourse
    * @Description: 课程上架
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/8/14 16:23
    * @Parameters: [courseId]
    * @Return cn.sunshine.ymcc.result.JSONResult
    */
    @RequestMapping(value="/onLineCourse/{courseId}",method= RequestMethod.POST)
    @PreAuthorize("hasAuthority('course:online')")
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }
    /*
    * @Title: offLineCourse
    * @Description: 课程下架
    * @Author: Mr.Jiang
    * @Version: 1.0
    * @Date:  2022/8/14 17:04
    * @Parameters: [courseId]
    * @Return cn.sunshine.ymcc.result.JSONResult
    */
    @RequestMapping(value="/offLineCourse/{courseId}",method= RequestMethod.POST)
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId){
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }
    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult save(@RequestBody @Valid CourseAddDto courseAddDto){
            courseService.save(courseAddDto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('course:list')")
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,wrapper);
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
